-- Language server configurations

if os.getenv('NVIM_DO_NOT_SETUP_LSP') == 'true' then
    return
end

local capabilities = require('cmp_nvim_lsp').default_capabilities()
local lspconfig = require('lspconfig')
-- Server-specific settings. See `:help lspconfig-setup`
lspconfig.lua_ls.setup {
    on_init = function(client)
        if client.workspace_folders then
            local path = client.workspace_folders[1].name
            if vim.uv.fs_stat(path .. '/.luarc.json') or vim.uv.fs_stat(path .. '/.luarc.jsonc') then
                return
            end
        end

        client.config.settings.Lua = vim.tbl_deep_extend('force', client.config.settings.Lua, {
            runtime = {
                version = 'LuaJIT'
            },
            -- Make the server aware of Neovim runtime files
            workspace = {
                checkThirdParty = false,
                library = {
                    vim.env.VIMRUNTIME,
                    -- Depending on the usage, you might want to add additional paths here.
                    "${3rd}/luv/library"
                    -- "${3rd}/busted/library",
                }
            }
        })
    end,
    settings = {
        Lua = {}
    }
}
lspconfig.rust_analyzer.setup {
    capabilities = capabilities,
    settings = {
        ['rust-analyzer'] = {
            inlayHints = {
                maxLength = 1000,
            }
        },
    },
}
lspconfig.clangd.setup {
    capabilities = capabilities,
}
lspconfig.elixirls.setup {
    capabilities = capabilities,
    cmd = { 'elixir-ls' }
}
lspconfig.pyright.setup {
    capabilities = capabilities,
}
lspconfig.gopls.setup {
    capabilities = capabilities,
    settings = {
        gopls = {
            hints = {
                assignVariableTypes = true,
                compositeLiteralFields = true,
                compositeLiteralTypes = true,
                constantValues = true,
                functionTypeParameters = true,
                parameterNames = true,
                rangeVariableTypes = true
            }
        }
    }
}
local tsserver_inlayhint_setup = {
    includeInlayParameterNameHints = 'all',
    includeInlayParameterNameHintsWhenArgumentMatchesName = false,
    includeInlayFunctionParameterTypeHints = true,
    includeInlayVariableTypeHints = true,
    includeInlayVariableTypeHintsWhenTypeMatchesName = false,
    includeInlayPropertyDeclarationTypeHints = true,
    includeInlayFunctionLikeReturnTypeHints = true,
    includeInlayEnumMemberValueHints = true,
}
lspconfig.ts_ls.setup {
    capabilities = capabilities,
    settings = {
        typescript = {
            inlayHints = tsserver_inlayhint_setup
        },
        javascript = {
            inlayHints = tsserver_inlayhint_setup
        }
    }
}
lspconfig.hls.setup {
    capabilities = capabilities
}
lspconfig.ocamllsp.setup {
    capabilities = capabilities
}
lspconfig.zls.setup {}
